if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "slam"

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= false
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 75
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes	= false
	
	SWEP.IconFont = "CSSelectIcons"
	
	SWEP.PrintName = "Ammokit"
	SWEP.Slot = 1
	SWEP.Slotpos = 1
	SWEP.IconLetter = "N"

end

SWEP.Base = "tp_base"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.Anim1 = ACT_VM_PRIMARYATTACK
SWEP.Anim2 = ACT_VM_SECONDARYATTACK
SWEP.Anim1Speed = 2.1
SWEP.Anim2Speed = .8

SWEP.ViewModel = "models/weapons/v_c4.mdl"
SWEP.WorldModel = "models/weapons/w_c4.mdl" 

SWEP.Deny = Sound("SuitRecharge.Deny")
SWEP.GiveSound = Sound("buttons/weapon_confirm.wav")

SWEP.CurPlayer = nil
SWEP.Mode = 0

function SWEP:Initialize()
    if (SERVER) then
		self:SetWeaponHoldType(self.HoldType)
	end
end 

function SWEP:GetUsedAmmoType()
	return "none"
end

function SWEP:Deploy()
	if not self.Owner.PlayerTable then self.Owner.PlayerTable = {} end
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	return true
end  

function SWEP:DrawHUD()
	local w,h = ScrW(),ScrH()
	
	surface.SetDrawColor(70, 80, 220, 220)
	local wh, lh, sh = w*.5, h*.5, 10
	surface.DrawLine(wh - sh, lh - sh, wh + sh, lh - sh) //top line
	surface.DrawLine(wh - sh, lh + sh, wh + sh, lh + sh) //bottom line
	surface.DrawLine(wh - sh, lh - sh, wh - sh, lh + sh) //left line
	surface.DrawLine(wh + sh, lh - sh, wh + sh, lh + sh) //right line
end

function SWEP:CanPrimaryAttack()
	return true
end

function SWEP:PrimaryAttack()
	if CLIENT then return end

	local checkpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 70
	local len = 100
	local curplayer
	for k,v in pairs(team.GetPlayers(TEAM_HUMANS)) do
		if v:GetPos():Distance(checkpos) < len and v != self.Owner then
			len = v:GetPos():Distance(checkpos)
			curplayer = v
		end
	end
	   
	if curplayer then
		if not self.Owner.PlayerTable then self.Owner.PlayerTable = {} end
		if table.HasValue(self.Owner.PlayerTable,curplayer) then
			self.Owner:EmitSound(self.Deny)
			Inventory.Notice(self.Owner, "You already resupplied that person", Inventory.Red)
			self.Weapon:SetNextPrimaryFire( CurTime() + .5 )
		else
			local wep = curplayer:GetActiveWeapon()
			local atype = wep:GetUsedAmmoType()
			if self.Types[atype] then
				self.TypeAmt = self.Types[atype]
				self.TypeName = atype
				self.CurPlayer = curplayer
				self.Mode = 1
				self.Timer = CurTime() + self.Anim1Speed
				self.Weapon:SendWeaponAnim(self.Anim1)
				self.Weapon:SetNextPrimaryFire( CurTime() + 2 )
			else
				self.Owner:EmitSound(self.Deny)
				Inventory.Notice(self.Owner, "That person isn't holding a weapon", Inventory.Red)
				self.Weapon:SetNextPrimaryFire( CurTime() + .5 )
			end
		end
	else
		self.Owner:EmitSound(self.Deny)
		Inventory.Notice(self.Owner, "You're too far away", Inventory.Red)
		self.Weapon:SetNextPrimaryFire( CurTime() + .5 )
	end
end

SWEP.Types = {}
SWEP.Types["pistol"] = 12
SWEP.Types["smg1"] = 15
SWEP.Types["buckshot"] = 8
SWEP.Types["ar2"] = 15

function SWEP:AmmoGive()
	if self.CurPlayer:IsValid() and SERVER then
		table.insert(self.Owner.PlayerTable,self.CurPlayer)
		self.CurPlayer:AddAmmunition(self.TypeName,self.TypeAmt)
		Inventory.Notice(self.CurPlayer, self.Owner:Name().." gave you some ammo", Inventory.Blue)
		Inventory.Notice(self.Owner, "You supplied ammo to "..self.CurPlayer:Name(), Inventory.Green)
		self.Owner:EmitSound(self.GiveSound)
		self.Owner:EmitSound(Inventory.AmmoPickup,100,math.random(90,100))
		self.Owner:VoiceSound(ChooseRandom(self.Owner.Supply),100,.3)
		self.Owner:AddAwardPoints(AWARD_GOOD_SUPPORT,1)
		if not self.Owner.AmmoHint then
			self.Owner.AmmoHint = true
			Inventory.Notice(self.Owner, "Resupply "..math.Clamp(team.NumPlayers(TEAM_HUMANS) - 1,1,3).." allies to earn points", Inventory.White)
		end
		if table.Count(self.Owner.PlayerTable) >= math.Clamp(team.NumPlayers(TEAM_HUMANS) - 1,1,3) then
			self.Owner.PlayerTable = {}
			Inventory.Notice(self.Owner, "You supplied ammo to "..math.Clamp(team.NumPlayers(TEAM_HUMANS) - 1,1,3).." allies", Inventory.Green)
			self.Owner:AddPoints(math.Clamp(team.NumPlayers(TEAM_HUMANS),1,3))
		end
	else
		self.Owner:EmitSound(self.Deny)
	end
end

function SWEP:Think()

	if self.Mode == 0 then return end
	if self.Mode == 1 and self.Timer < CurTime() then
		self.Mode = 2 
		self.Timer = CurTime() + self.Anim2Speed
		self.Weapon:SendWeaponAnim(self.Anim2)
		self:AmmoGive()
	elseif self.Mode == 2 and self.Timer < CurTime() then
		self.Mode = 0
		self.Timer = nil
		self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	end

end
 
function SWEP:SecondaryAttack()

end

